<?php
/**
 * Created by PhpStorm.
 * User: figo-007
 * Date: 2016/11/30
 * Time: 19:32
 */
namespace ApigilityOrder\Service;

use Zend\ServiceManager\ServiceManager;
use Zend\Hydrator\ClassMethods as ClassMethodsHydrator;
use ApigilityCatworkFoundation\Base\ApigilityEventAwareObject;

class CouponPaymentService extends ApigilityEventAwareObject
{

    protected $classMethodsHydrator;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $em;

    /**
     * @var \ApigilityOrder\Service\OrderService
     */
    protected $orderService;
    /**
     * @var \ApigilityUser\Service\UserService
     */
    protected $userService;

    /**
     * @var \ApigilityVIP\Service\ContractService
     */
    protected $contractService;

    /**
     * @var \ApigilitySocial\Service\PersonService
     */
    protected $personService;

    public function __construct(ServiceManager $services)
    {
        $this->classMethodsHydrator = new ClassMethodsHydrator();
        $this->em = $services->get('Doctrine\ORM\EntityManager');
        $this->orderService = $services->get('ApigilityOrder\Service\OrderService');
        $this->userService = $services->get('ApigilityUser\Service\UserService');
        $this->contractService = $services->get('ApigilityVIP\Service\ContractService');
        $this->personService = $services->get('ApigilitySocial\Service\PersonService');
    }


    public function handleCouponPayment($data)
    {
        $order_sn = null;
        if (isset($data->seriesNumber))
            $order_sn = $data->seriesNumber;

        $orderService = $this->orderService;
        $em = $this->em;
        $order = $orderService->getOrderBySN($order_sn);
        $order->setStatus($order::STATUS_PAYED);
        $order->setPaymentType('couponpay');
        $order->setPayTime(new \DateTime());

        $u = $this->userService->getUser($order->getUser()->getId());
        $contract = $this->contractService->getContractByOrderId($order->getId());
        $contracts = $this->contractService->getContractsByUser($u); // 获取某个用户已有的合约列表
        $expire_time = count($contracts) ? (new \DateTime())->setTimestamp($contracts[0]->getExpireTime()->getTimestamp() + $contract->getService()->getExpire()) :
            (new \DateTime())->setTimestamp((new \DateTime())->getTimestamp() + $contract->getService()->getExpire());

        $person = $this->personService->getPersonByUserId($u->getId());
        $person->setVipStatus($contract->getService()->getStatus());
        $person->setVipExpireTime($contract->getExpireTime());
        $em->flush();
        if($person->getVipExpireTime() < $expire_time && count($contracts)>0){
            $contract->setEffectiveTime(count($contracts) ? $contracts[0]->getExpireTime() : $contract->getEffectiveTime());
            $contract->setExpireTime($expire_time);
            $em->flush();
        }

        $coupon = $this->em->getRepository('ApigilityCoupon\DoctrineEntity\Coupon')
            ->findOneBy(array('id' => $data->couponId));
        $this->em->remove($coupon);
        $this->em->flush($coupon);
    }
}